package com.doitedu.mr.day05.index;

import com.doitedu.mr.day05.WordCountMapper;
import com.doitedu.mr.day05.WordCountReducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * @Date 2021/12/6
 * @Created by HANGGE
 * @Description TODO
 */
public class Index01 {
    /**
     * 输出的结果是 单词-文件名   1
     */
    static  class  Index01Mapper extends Mapper<LongWritable , Text , Text , IntWritable>{
        String fileName = null ;
        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            // 获取任务切片
            FileSplit fileSplit = (FileSplit)context.getInputSplit();
            // 获取文件名
             fileName = fileSplit.getPath().getName();
        }
  Text k = new Text() ;
        IntWritable v =   new IntWritable(1);
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] words = line.split("\\s+");
            for (String word : words) {
                String wordAndFileName = word+"-"+fileName ;
                k.set(wordAndFileName);
                context.write(k,v);
            }

        }
    }

    static class  Index01Reducer extends Reducer<Text , IntWritable ,Text, IntWritable>{
        IntWritable v = new IntWritable() ;
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int cnt = 0 ;
            for (IntWritable value : values) {
                cnt++ ;
            }
            v.set(cnt);
            context.write(key , v);
        }
    }

    public static void main(String[] args) throws  Exception {
        // Job  统计文件中单词出现的次数
        Configuration conf = new Configuration();
        // 1  创建 JOB
        Job job = Job.getInstance(conf, "index01");
        // 1) Mapper 类
        job.setMapperClass(Index01Mapper.class);
        // 2) Reducer类
        job.setReducerClass(Index01Reducer.class);
        // 3)  当map端和reduce端的输出类型完全一致 可以省略
      /*  job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);*/

        // 4)  设置reduce输出的数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        job.setNumReduceTasks(2);
        // 5) 设置输入的数据路径
        FileInputFormat.setInputPaths(job , new Path("E:\\mrdata\\index\\input"));
        // 6) 设置输出的结果保存路径
        FileOutputFormat.setOutputPath(job, new Path("E:\\mrdata\\index\\out01"));
        // 2 提交工作
        // 等待执行完成 , 打印执行过
        job.waitForCompletion(true) ;



    }
}
